//
//  SAXMLDictionaryParser.m
//  GameFrame
//
//  Created by Hiroshi Nomura on 2014/05/24.
//  Copyright (c) 2014年 Hiroshi Nomura. All rights reserved.
//

#import "SAXMLDictionaryParser.h"

@interface SAXMLDictionaryParser()

-(id)initDictionary;

@property (nonatomic, retain)NSMutableDictionary* resDictionary;

@end

@implementation SAXMLDictionaryParser

@synthesize resDictionary;

+(NSDictionary*)parseXMLfile:(NSString*)filename
{
    NSString* filePath		= [[NSBundle mainBundle] pathForResource:filename ofType:@"xml"];
    NSFileManager*fm		= [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:filePath]) {
        return nil;
    }

    NSURL *url = [NSURL fileURLWithPath:filename];
    NSXMLParser* parser   = [[NSXMLParser alloc] initWithContentsOfURL:url];
    
    SAXMLDictionaryParser* dictionaryParser = [[SAXMLDictionaryParser init] alloc];
    [parser setDelegate:dictionaryParser];
    
    if ([parser parse]) {
        NSDictionary* dict = [NSDictionary dictionaryWithDictionary:dictionaryParser.resDictionary];
        return dict;
    }
    
    return nil;
}

-(id)initDictionary
{
	if((self = [super init]))
    {
        self.resDictionary = [[NSMutableDictionary init] alloc];
    }
    return self;
}

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

}

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{

}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    
}

@end
